package com.atguigu.juc.study.chapter09.atomics;

import java.util.concurrent.CountDownLatch;

/**
 * @author : leimingchao
 * @date Date : 2022年06月15日 0:36
 */
public class AtomicIntegerDemo {

    public static final int SIZE = 50;

    public static void main(String[] args) {

        MyNumber myNumber = new MyNumber();

        CountDownLatch countDownLatch = new CountDownLatch(50);

        for (int i = 1; i <= SIZE; i++) {
            new Thread(() -> {

                try {
                    for (int j = 0; j < 1000; j++) {
                        myNumber.addPlusPlus();
                    }
                } finally {
                    countDownLatch.countDown();
                }
            }, String.valueOf(i)).start();
        }

        // try {
        //     TimeUnit.SECONDS.sleep(2);
        // } catch (InterruptedException e) {
        //     e.printStackTrace();
        // }

        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }


        System.out.println(Thread.currentThread().getName() + "\t" + "result:" +myNumber.getAtomicInteger().get());
    }
}
